var express = require('express')
var router = express.Router()
var requestHandlers = require("./requestHandlers")
var fs=require("fs")


require('./functions.js')


/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'index' , tips: "choose a operation:"})
})

router.get('/clearOfferCache', function(req, res, next) {
  requestHandlers.offers_G = []
  res.redirect('/')
})

router.get('/uploadCreative', function(req, res, next) {
    requestHandlers.displayUploadCreative(res)
})

router.get('/updateCreativeLink2', function(req, res, next) {
    requestHandlers.displayUpdateCreativeLink2(res)
})

router.get('/config', function(req, res, next) {
  	var config = {api_urls:'多个连接"|"隔开', adv_camp_id:'如果adv_camp_id是offer索引这里填: campIdIsIdx', api_bodyDir:'这里填api主体路径,例如yeahmobi填data.data', advertiser_id:'必填', creative_urlDir:'上级是creative_link如yeahmobi填link,avazu特例：无000，avazu001'}
  	res.render('config', { title: 'config', config:config })
})

router.get('/partnerList', function(req, res, next) {
	var html =
	'<html>'+
    '<head>'+
    '<meta http-equiv="Content-Type" '+
    'content="text/html; charset=UTF-8" />'+
	'<link rel="stylesheet" href="/stylesheets/style.css" />'+
    '</head>'+
    '<body>'+
    '<p><a href="/">首页</a></p>'+
    'choose a advertiser_id:<br><br>'
    
    var files = fs.readdirSync('partnerConfigs/')
    for(var i in files)
    {	
    	if (files[i]=='.DS_Store') continue
    	var advertiser_id = files[i].substring(0,files[i].length - 5)
    	var h = '<a href="/offerList/?advertiser_id=' + advertiser_id + '&pageIndex=1&isAll=true">' + advertiser_id + '</a>'
    	html = html + h + '<br><br>'
    }
    html = html + '</body></html>' 

    res.write(html)
    res.end()
})

router.get('/offerList', function(req, res, next) {

    requestHandlers.dealOfferList(req, res)

})

router.post('/offerList', function(req, res, next) {

    requestHandlers.dealOfferList(req, res)

})

router.post('/config', 
function(req, res, next) 
{
	if(req.body.top_advertiser_id)
	{	
		var filePath = 'partnerConfigs/' + req.body.top_advertiser_id + '.json'
		if( fs.existsSync(filePath) )
		{
			var config = fs.readFileSync(filePath).toString()
			res.render('config', { title: 'config', config:JSON.parse(config) })
		}
	}
	else if(req.body.advertiser_id)
	{	
		var filePath = 'partnerConfigs/' + req.body.advertiser_id + '.json'
		req.body.advertiser_id = null
		fs.writeFile(filePath, JSON.stringify(req.body))
		res.render('index', { title: 'index' , tips: "choose a operation:"})
	}
	else 
	{
		res.redirect('/config')
	}
})


router.post('/uploadCreative', 
function(req, res, next) 
{
    requestHandlers.dealUploadCreativePost(req, res)   
})

router.post('/updateCreativeLink2', 
function(req, res, next) 
{
    requestHandlers.dealUpdateCreativeLink2(req, res)   
})


router.get('/addToSql', function(req, res, next) {

    requestHandlers.dealAddToSql(req, res) 

})


router.post('/addToSql', function(req, res, next) {

    requestHandlers.dealAddToSql(req, res) 

})


module.exports = router
